package com.zuthub.web.controller.system;

import cn.hutool.core.date.DateUtil;
import com.zuthub.common.core.domain.AjaxResult;
import com.zuthub.common.utils.DateUtils;
import com.zuthub.common.utils.StringUtils;
import com.zuthub.system.domain.SysMember;
import com.zuthub.system.domain.SysSponsor;
import com.zuthub.system.domain.SysSponsorship;
import com.zuthub.system.domain.vo.AnalyseVo;
import com.zuthub.system.domain.vo.MemberChangeVo;
import com.zuthub.system.domain.vo.SponsorShipVo;
import com.zuthub.system.service.ISysMemberService;
import com.zuthub.system.service.ISysSponsorService;
import com.zuthub.system.service.ISysSponsorshipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 统计分析模块
 */
@RestController
@RequestMapping("system/analyse")
public class SysAnalyseController {

    @Autowired
    private ISysMemberService memberService;

    @Autowired
    private ISysSponsorService sysSponsorService;

    @Autowired
    private ISysSponsorshipService sysSponsorshipService;

    @GetMapping("memberGender")
    public AjaxResult getMemberGenderAnalyseResult() {
        List<SysMember> sysMembers = memberService.selectSysMemberList(null);
        long manCount = sysMembers.stream().filter(sysMember -> {
            return "男".equals(sysMember.getMemberGender());
        }).count();
        long womanCount = sysMembers.stream().filter(sysMember -> {
            return "女".equals(sysMember.getMemberGender());
        }).count();
        List<AnalyseVo> list = new ArrayList<>();
        list.add(new AnalyseVo((int) manCount, "男"));
        list.add(new AnalyseVo((int) womanCount, "女"));
        return AjaxResult.success(list);
    }

    @GetMapping("memberDistribute")
    public AjaxResult getMemberDistribute() {
        List<SysMember> sysMembers = memberService.selectSysMemberList(null);
        long wailian = sysMembers.stream().filter(sysMember -> {
            return "外联部".equals(sysMember.getDept());
        }).count();
        long xinxi = sysMembers.stream().filter(sysMember -> {
            return "信息部".equals(sysMember.getDept());
        }).count();
        long zuzhi = sysMembers.stream().filter(sysMember -> {
            return "组织部".equals(sysMember.getDept());
        }).count();
        long xuanchuan = sysMembers.stream().filter(sysMember -> {
            return "宣传部".equals(sysMember.getDept());
        }).count();
        long qita = sysMembers.stream().filter(sysMember -> {
            return StringUtils.isEmpty(sysMember.getDept());
        }).count();
        List<AnalyseVo> list = new ArrayList<>();
        list.add(new AnalyseVo((int) wailian, "外联部"));
        list.add(new AnalyseVo((int) xinxi, "信息部"));
        list.add(new AnalyseVo((int) xuanchuan, "宣传部"));
        list.add(new AnalyseVo((int) zuzhi, "组织部"));
        list.add(new AnalyseVo((int) qita, "其他"));
        return AjaxResult.success(list);
    }

    @GetMapping("memberChange")
    public AjaxResult getMemberChange(){
        int currentYear = DateUtil.year(new Date());
        int oneYearPast = currentYear-1;
        int twoYearPast = currentYear-2;
        int threeYearPast = currentYear-3;
        List<SysMember> sysMembers = memberService.selectSysMemberList(null);
        List<String>  nameList = new ArrayList<>();
        List<Long>  valueList = new ArrayList<>();
        long currentYearCount = sysMembers.stream()
                .map(SysMember::getCreateTime)
                .map(DateUtil::year)
                .filter(year -> year.equals(currentYear))
                .count();
        long oneYearPastCount = sysMembers.stream()
                .map(SysMember::getCreateTime)
                .map(DateUtil::year)
                .filter(year -> year.equals(oneYearPast))
                .count();
        long twoYearPastCount = sysMembers.stream()
                .map(SysMember::getCreateTime)
                .map(DateUtil::year)
                .filter(year -> year.equals(twoYearPast))
                .count();
        long threeYearPastCount = sysMembers.stream()
                .map(SysMember::getCreateTime)
                .map(DateUtil::year)
                .filter(year -> year.equals(threeYearPast))
                .count();
        nameList.add(String.valueOf(threeYearPast));
        nameList.add(String.valueOf(twoYearPast));
        nameList.add(String.valueOf(oneYearPast));
        nameList.add(String.valueOf(currentYear));
        valueList.add(threeYearPastCount);
        valueList.add(twoYearPastCount);
        valueList.add(oneYearPastCount);
        valueList.add(currentYearCount);
        MemberChangeVo memberChangeVo = new MemberChangeVo();
        memberChangeVo.setNameList(nameList);
        memberChangeVo.setValueList(valueList);
        return AjaxResult.success(memberChangeVo);
    }

    @GetMapping("sponsorShipDistribute")
    public AjaxResult getSponsorShipDistribute() {
        int year = DateUtil.year(new Date());
        List<SysSponsor> sponsors = sysSponsorService.selectSysSponsorList(null);
        List<SysSponsorship> sponsorships = sysSponsorshipService.selectSysSponsorshipList(null);
        List<String> sponsorNameList = sponsors.stream()
                .filter(sysSponsor ->DateUtil.year(sysSponsor.getCreateTime())==year)
                .map(SysSponsor::getName)
                .collect(Collectors.toList());
        List<Long> valueList = sponsors.stream()
                .filter(sysSponsor ->DateUtil.year(sysSponsor.getCreateTime())==year)
                .map(sysSponsor ->
                     sponsorships.stream()
                             .filter(ship ->DateUtil.year(ship.getCreateTime())==year)
                             .filter(ship -> sysSponsor.getSponsorId().equals(ship.getSponsorId()))
                             .mapToLong(SysSponsorship::getMoney)
                             .sum())
                .collect(Collectors.toList());
        SponsorShipVo sponsorShipVo = new SponsorShipVo();
        sponsorShipVo.setNameList(sponsorNameList);
        sponsorShipVo.setValueList(valueList);
        return AjaxResult.success(sponsorShipVo);
    }
}
